#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_pushButton_clicked()
{
    pix.load(":/new/prefix1/支付宝.jpeg");
    ui->label_4->setPixmap(pix);
    ui->label_4->setScaledContents(true);
}

void Widget::on_pushButton_2_clicked()
{
    QString pdfName="C://Users//Admin//soft//Qt//qt10//output.pdf";
    QString pdfPath=QFileDialog::getSaveFileName(this,"导出PDF文件",pdfName,"*.pdf");//保存pdf文件路径

    QFile pdffile(pdfPath);//将文件路径加载到文件对象
    if(pdffile.open(QIODevice::WriteOnly))//以只写的形式打开文件
    {
        QPdfWriter * writer=new QPdfWriter(&pdffile);

        writer->setPageSize(QPagedPaintDevice::A4);
        writer->setResolution(300);//设置DPI
        writer->setPageMargins(QMarginsF(20,20,20,20));//设置边距

        QPainter * painter=new QPainter(writer);
        painter->setFont(QFont("楷体",13));

        //开始写入内容
        //写入文字
        int start=10;
        painter->drawText(QRect(0,start,writer->width(),100),Qt::AlignCenter,"您好！Hello");


        //画线
        start+=120;
        painter->setPen(QPen(QBrush(QColor(0,0,0)),5));
        painter->drawLine(0,start,writer->width(),start);

        //文字
         painter->drawText(QRect(0,170,writer->width(),100),Qt::AlignLeft,"姓名:每丹阳  性别：女  年龄：21");
         painter->drawText(QRect(0,220,writer->width(),100),Qt::AlignLeft,"毕业院校：宝鸡文理学院  专业：软件工程");
         painter->drawText(QRect(0,280,writer->width(),100),Qt::AlignLeft,"籍贯：陕西省西安市长安区");


        //画图
        start+=120;
        QPixmap image(":/new/prefix1/支付宝.jpeg");
        painter->drawPixmap(1000,start,image.width(),image.height(),image);

        //画线
        start+=220;
        painter->setPen(QPen(QBrush(QColor(0,0,0)),5));
        painter->drawLine(0,start,writer->width(),start);

        delete painter;
        painter=nullptr;
        delete writer;
        writer=nullptr;

        pdffile.close();
    }
}
